package com.yudean.esb.service.impl.mds;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yudean.esb.domain.mds.MdsLnfinData;
import com.yudean.esb.mapper.mds.MdsLnfinDataMapper;
import com.yudean.esb.service.MdsLnfinDataService;
import com.yudean.esb.util.StringUtil;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;


@Service("mdsLnfinDataService")
public class MdsLnfinDataServiceImpl  extends ServiceImpl<MdsLnfinDataMapper, MdsLnfinData> implements MdsLnfinDataService {
    @Resource
    private MdsLnfinDataMapper mdsLnfinDataMapper;


    /**
     * 分页查询数据
     * @param map
     * @param pageNoInt
     * @param pageSizeInt
     * @param formName
     * @param accountName
     * @return
     */
    @Override
    public String getLnFinDataByPage(Map<String,String> map, long pageNoInt, long pageSizeInt,String formName,String accountName) {
        //查询(可修改为LambdaQueryWrapper)
        QueryWrapper<MdsLnfinData> wrapper = new QueryWrapper<>();
        map.forEach(wrapper::eq);
        if (!StringUtil.isEmpty(formName)){
            wrapper.like("FORMNAME",formName);
        }
        if(!StringUtil.isEmpty(accountName)){
            wrapper.like("ACCOUNTNAME",accountName);
        }
        Page<MdsLnfinData> page = new Page<>(pageNoInt,pageSizeInt);
        mdsLnfinDataMapper.selectPage(page,wrapper);
        List<MdsLnfinData> records = page.getRecords();
        //总数
        long total = page.getTotal();
        if(records.size() == 0){
            return noData();
        }
        return parseLnFinDataList(records,String.valueOf(total),String.valueOf(pageNoInt),String.valueOf(pageSizeInt));
    }

    /**
     * 转成xml
     * @param list
     * @param total
     * @param pageNo
     * @param pageSize
     * @return
     */
    public String parseLnFinDataList(List<MdsLnfinData> list,String total,String pageNo,String pageSize){
        Document doc;
        doc = DocumentHelper.createDocument();
        Element root = doc.addElement("service");
        root.addElement("name").setText("ERP财务报表信息");
        root.addElement("type").setText("发布服务");
        if(!StringUtil.isEmpty(total) && !StringUtil.isEmpty(pageNo) && !StringUtil.isEmpty(pageSize)){
            root.addElement("total").setText(total);
            root.addElement("pageNo").setText(pageNo);
            root.addElement("pageSize").setText(pageSize);
        }
        Element dataList = root.addElement("datalist");
        try {
            for (MdsLnfinData finIndex : list) {
                if (finIndex == null) {
                    continue;
                }
                Element data = dataList.addElement("data");
                this.dataWrapper(finIndex, data);
            }
            return doc.asXML();
        } catch (Exception e) {
            log.error("parseList():error:" + e.getMessage(), e);
            return noData();
        }
    }


    /**
     * 将对象装配到Elment中去
     * @param finIndex
     * @param data
     */
    private  void dataWrapper(MdsLnfinData finIndex, Element data) {
        data.addElement("formId").setText(
                StringUtil.convertToString(finIndex.getFormid()));
        data.addElement("formName").setText(
                StringUtil.convertToString(finIndex.getFormname()));
        data.addElement("accountId").setText(
                StringUtil.convertToString(finIndex.getAccountid()));
        data.addElement("accountName").setText(
                StringUtil.convertToString(finIndex.getAccountname()));
        data.addElement("organisationElementId").setText(
                StringUtil.convertToString(finIndex.getOrganisationelementid()));
        data.addElement("orgnazationCode").setText(
                StringUtil.convertToString(finIndex.getOrgnazationcode()));
        data.addElement("organisationElementName").setText(
                StringUtil.convertToString(finIndex.getOrganisationelementname()));
        data.addElement("period").setText(
                StringUtil.convertToString(finIndex.getPeriod()));
        data.addElement("currency").setText(
                StringUtil.convertToString(finIndex.getCurrency()));
        data.addElement("dataLevel").setText(
                StringUtil.convertToString(finIndex.getDatalevel()));
        data.addElement("adjustmentLevel").setText(
                StringUtil.convertToString(finIndex.getAdjustmentlevel()));
        data.addElement("valueType").setText(
                StringUtil.convertToString(finIndex.getValuetype()));
        data.addElement("dataValue").setText(
                StringUtil.convertToString(finIndex.getDatavalue()));
        data.addElement("creationDate").setText(
                StringUtil.convertToString(DateUtil.formatDate(finIndex.getCreationdate())));
    }


    /**
     * 返回空数据的时候的信息
     *
     * @return
     */
    private static String noData() {
        return "<?xml version=\"1.0\" encoding=\"UTF-8\"?><service><name>ERP财务报表信息</name><type>发布服务</type><datalist><!--查无数据--></datalist></service>";
    }
}
